package views {
	import at.geoathome.ui.buttons.TextButton;
	

	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;	

	/**
	 * @author wolf
	 */
	public class GameSettingsView extends Sprite{
		private var _drawn:Boolean;
		private var _game:Game;
		
		
		private var _gravity_txt:TextField;
		private var _mass_of_objects_txt:TextField;
		private var _numberOf_addBox_gameA_txt:TextField;
		private var _numberOf_addBox_gameB_txt:TextField;
		private var _numberOf_moveBox_gameB_txt:TextField;
		
		private var _gravity:Number=new Number(9.81);
		private var _mass_of_objects:Number=new Number(1.7);
		private var _numberOf_addBox_gameA:Number=3;
		private var _numberOf_addBox_gameB:Number=5;
		private var _numberOf_moveBox_gameB:Number=1;
		
		private var _gravity_label:TextButton;
		private var _mass_of_objects_label:TextButton;
		private var _numberOf_addBox_gameA_label:TextButton;
		private var _numberOf_addBox_gameB_label:TextButton;
		private var _numberOf_moveBox_gameB_label:TextButton;
		private var _gameA_label:TextButton;
		private var _gameB_label:TextButton;
		
		private var _backButton:TextButton;
		
		
		
		// -- constructor --------------------------------------------------------------------------------------
		public function GameSettingsView(game:Game)
		{
			_game = game;
			_drawn = false;
			extractAssets();
			trace("im konstruktor_gamesettingsview");
			//hide();
		}
		
		
		// -- methods ------------------------------------------------------------------------------------------
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			//if (visible)
				//show();
		}
		
		private function extractAssets():void
		{
			
		}
		
		
		private function draw():void 
		{
			// number of gravity 
			_gravity_label=new TextButton();
			
			_gravity_label.y=18;
			_gravity_label.addLabel("Gravity: ", 0x000000, 14);
			_gravity_label.width=150;
			_gravity_label.x=200;
			addChild(_gravity_label);
			
			_gravity_txt=new TextField();
			_gravity_txt.x = 370;
			_gravity_txt.y = 10;
			_gravity_txt.type = TextFieldType.INPUT;
			_gravity_txt.text = _gravity.toString();
			_gravity_txt.border=true;
			_gravity_txt.height=20;
			addChild(_gravity_txt);
			
			_mass_of_objects_label=new TextButton();
			
			_mass_of_objects_label.y=48;
			_mass_of_objects_label.addLabel("Mass of Objects: ", 0x000000, 14);
			_mass_of_objects_label.width=150;
			_mass_of_objects_label.x = 200;
			addChild(_mass_of_objects_label);
			
			_mass_of_objects_txt = new TextField();
			_mass_of_objects_txt.x = 370;
			_mass_of_objects_txt.y = 40;
			_mass_of_objects_txt.border=true;
			_mass_of_objects_txt.height=20;
			_mass_of_objects_txt.type = TextFieldType.INPUT;
			_mass_of_objects_txt.text = _mass_of_objects.toString();
			addChild(_mass_of_objects_txt);
			
			_gameA_label = new TextButton();
			_gameA_label.y=120;
			_gameA_label.addLabel("Game A ",0x000000,20);
			_gameA_label.width=150;
			_gameA_label.x=180;
			addChild(_gameA_label);
			
			
			_numberOf_addBox_gameA_label=new TextButton();
			_numberOf_addBox_gameA_label.y=158;
			_numberOf_addBox_gameA_label.addLabel("Number of Add-Actions: ",0x000000,14);
			_numberOf_addBox_gameA_label.width=150;
			_numberOf_addBox_gameA_label.x=100;
			addChild(_numberOf_addBox_gameA_label);
			
			_numberOf_addBox_gameA_txt=new TextField();
			_numberOf_addBox_gameA_txt.x = 250;
			_numberOf_addBox_gameA_txt.y = 150;
			_numberOf_addBox_gameA_txt.border=true;
			_numberOf_addBox_gameA_txt.height=20;
			_numberOf_addBox_gameA_txt.type = TextFieldType.INPUT;
			_numberOf_addBox_gameA_txt.text = _numberOf_addBox_gameA.toString();
			addChild(_numberOf_addBox_gameA_txt);
			
			_gameB_label = new TextButton();
			_gameB_label.y=120;
			_gameB_label.addLabel("Game B ",0x000000,20);
			_gameB_label.width=150;
			_gameB_label.x=450;
			addChild(_gameB_label);
			
			_numberOf_addBox_gameB_label=new TextButton();
			_numberOf_addBox_gameB_label.y=158;
			_numberOf_addBox_gameB_label.addLabel("Number of Add-Actions: ",0x000000,14);
			_numberOf_addBox_gameB_label.width=150;
			_numberOf_addBox_gameB_label.x=410;
			addChild(_numberOf_addBox_gameB_label);
			
			_numberOf_addBox_gameB_txt=new TextField();
			_numberOf_addBox_gameB_txt.x = 580;
			_numberOf_addBox_gameB_txt.y = 150;
			_numberOf_addBox_gameB_txt.border=true;
			_numberOf_addBox_gameB_txt.height=20;
			_numberOf_addBox_gameB_txt.type = TextFieldType.INPUT;
			_numberOf_addBox_gameB_txt.text = _numberOf_addBox_gameB.toString();
			addChild(_numberOf_addBox_gameB_txt);
			
			_numberOf_moveBox_gameB_label=new TextButton();
			_numberOf_moveBox_gameB_label.y = 188;
			
			_numberOf_moveBox_gameB_label.addLabel("Number of Move-Actions: ",0x000000,14);
			_numberOf_moveBox_gameB_label.width=170;
			_numberOf_moveBox_gameB_label.x = 410;
			addChild(_numberOf_moveBox_gameB_label);
			
			_numberOf_moveBox_gameB_txt=new TextField();
			_numberOf_moveBox_gameB_txt.x = 580;
			_numberOf_moveBox_gameB_txt.y = 180;
			_numberOf_moveBox_gameB_txt.type = TextFieldType.INPUT;
			_numberOf_moveBox_gameB_txt.border=true;
			_numberOf_moveBox_gameB_txt.height=20;
			_numberOf_moveBox_gameB_txt.text = _numberOf_moveBox_gameB.toString();
			addChild(_numberOf_moveBox_gameB_txt);
			
			_backButton=new TextButton();
			_backButton.x = 700;
			_backButton.y = 580;
			_backButton.addEventListener(MouseEvent.MOUSE_DOWN, onBackPressed);
			_backButton.addLabel("Back to Menu", 0x000000, 14);
			addChild(_backButton);
			
			
			trace("drawn_settings");
			_drawn = true;
		}
		
		private static function roundToPrecision(number:Number, precision:int = 0):Number
		{
			var decimalPlaces:Number = Math.pow(10, precision);
			return Math.round(decimalPlaces * number) / decimalPlaces;
		}
		
		
		private function onBackPressed(e:MouseEvent):void {
			_gravity = roundToPrecision(new Number(_gravity_txt.text), 2);
			_mass_of_objects = roundToPrecision(new Number(_mass_of_objects_txt.text),2);
			_numberOf_addBox_gameA=new Number(_numberOf_addBox_gameA_txt.text);
			_numberOf_addBox_gameB=new Number(_numberOf_addBox_gameB_txt.text);
			_numberOf_moveBox_gameB=new Number(_numberOf_moveBox_gameB_txt.text);
			_game.changeStateToStartScreen();
			hide();
			
		}

		public function show():void 
		{
			trace("hier");
			draw();
			
			visible = true;
			
		}
		
	
		
		public function hide():void 
		{
			visible = false;
		}
	
	}
	
}